package bjtu.wms.wmstest.controller;

import bjtu.wms.wmstest.entity.Orders;
import bjtu.wms.wmstest.service.OrdersService;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.springframework.context.annotation.Bean;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Orders)表控制层
 *
 * @author makejava
 * @since 2023-05-25 15:13:03
 */
@Controller
//@RequestMapping("v1/orders")
public class OrdersController {
    /**
     * 服务对象
     */
    @Resource
    private OrdersService ordersService;



    @GetMapping("amount/{id}")
    public List<Orders> getOrderAmount(@PathVariable("id") Integer id) {
        return this.ordersService.getOrderAmount(id);
    }

    @GetMapping("state/{id}")
    public List<Orders> getOrderState(@PathVariable("id") Integer id) {
        return this.ordersService.getOrderState(id);
    }
    /**
     * 新增数据
     *
     * @param orders 实体
     * @return 新增结果
     */
    @PostMapping("/addorder")
    public String add(@ModelAttribute("orders") Orders orders) {
        this.ordersService.insert(orders);
        // 重定向到数据列表页面
        return "redirect:/order";
    }

    @GetMapping("/delete/{id}")
    public String deleteById(@PathVariable Integer id) {
        this.ordersService.deleteById(id);
        return "redirect:/order";
    }

    @GetMapping("/delete_user/{id}")
    public String deleteById_user(@PathVariable Integer id) {
        this.ordersService.deleteById(id);
        return "redirect:/order_user";
    }

//获取后端数据
    @GetMapping("/order")
    public String showData(Model model){
        List<Orders> dataList = ordersService.findAll();
        model.addAttribute("dataList", dataList);
        return "order";
    }
    @GetMapping("/order_user")
    public String showData_user(Model model){
        List<Orders> dataList = ordersService.findAll();
        model.addAttribute("dataList", dataList);
        return "order_user";
    }
    //跳转到新增页面
    @GetMapping("/addorder")
    public String addorder(Model model){
        List<Orders> dataList = ordersService.findAll();
        model.addAttribute("dataList", dataList);
        return "addorder";
    }

    //跳转到编辑页面
//    @GetMapping("/orders/{id}")
//    public String editPage(@PathVariable("id") Integer id, Model model) {
//        // 根据 id 获取对应的订单，并将其加入 model
//        Orders order = ordersService.queryById(id);
//        if (order == null) {
//            throw new ResourceNotFoundException("订单不存在，id=" + id);
//        }
//        model.addAttribute("order", order);
//        return "edit-order";
//    }
//
//    @PutMapping("/edit")
//    public ResponseEntity<Boolean> edit(@RequestBody Orders orders) {
//        return ResponseEntity.ok(this.ordersService.update(orders));
//    }
}

